﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace InnovTech.Util.VS
{
    public class VSProject
    {

        private static readonly string PROJECT_NAME_EXTENSION = "csproj";
        private static readonly int PROJECT_NAME_MIN_LENGTH = 8;

        public static string GetProjectName(string projFullName)
        {
            return Path.GetFileNameWithoutExtension(projFullName);
        }

        public static List<VSProject> GetProjects(string sourceCodePath)
        {
            List<string> projPathList =
                FileUtil.GetFiles(sourceCodePath, name => name.Length > PROJECT_NAME_MIN_LENGTH && 
                                                name.EndsWith("." + PROJECT_NAME_EXTENSION));
            return (from projPath in projPathList
                    select new VSProject() { FullPath = projPath }).ToList();
        }


        private string _fullPath;

        public VSProject()
        {
        }

        public VSProject(string projPath)
        {
            FullPath = projPath;
        }

        public string Name { get; private set; }
        public string Directory { get; private set; }
        public string FullPath 
        {
            get
            {
                return _fullPath;
            }
            protected set
            {
                if (_fullPath != value)
                {
                    Ensure.FileExists(value);
                    _fullPath = value;
                    Name = GetProjectName(_fullPath);
                    Directory = Path.GetDirectoryName(FullPath);
                }
            }
        }

        public List<string> GetFiles(Predicate<string> condition)
        {
            return FileUtil.GetFiles(Directory, condition);
        }
    }
}
